package es.uji.viselab.visualservoing;

import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JPanel;

import es.uji.viselab.ViSeLabException;
import es.uji.viselab.experiment.ExperimentParameters;
import es.uji.viselab.image.process.ImageProcessing;
import es.uji.viselab.image.source.ImageSource;
import es.uji.viselab.math.Vector2;
import es.uji.viselab.network.Client;
import es.uji.viselab.network.DataPackage;
import es.uji.viselab.network.EncoderDecoder;
import es.uji.viselab.network.links.VispEncoderDecoder;

public class VsVispControlInterface extends VisualServoingTask implements ActionListener {

	Client client;

	JPanel rootPanel;
	protected Label labelIP = new Label("IP:");
	protected TextField ip = new TextField(20);
	protected Label labelPort = new Label("port:");
	protected TextField port = new TextField(5);
	protected Button buttonConnectServer = new Button("Connect");

	public VsVispControlInterface() {
		constructPanel();
	}

	public VsVispControlInterface(ExperimentParameters ep) {
		// TODO Auto-generated constructor stub
	}

	protected void constructPanel() {

		rootPanel = new JPanel();
		rootPanel.setLayout(new BoxLayout(rootPanel, BoxLayout.PAGE_AXIS));

		JPanel line1 = new JPanel();
		line1.setLayout(new FlowLayout());
		((FlowLayout) line1.getLayout()).setAlignment(FlowLayout.CENTER);
		line1.add(labelIP);
		line1.add(ip);
		line1.add(labelPort);
		line1.add(port);

		JPanel line2 = new JPanel();
		line2.setLayout(new FlowLayout());
		((FlowLayout) line2.getLayout()).setAlignment(FlowLayout.CENTER);
		line2.add(buttonConnectServer);

		buttonConnectServer.addActionListener(this);

		rootPanel.add(line1);
		rootPanel.add(line2);

		InetAddress host = null;
		try {
			host = InetAddress.getLocalHost();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ip.setText(host.getHostAddress());
		port.setText("10101");
	}

	public JPanel getPanel() {
		return rootPanel;
	}

	@Override
	public void actionPerformed(ActionEvent ae) {
		if (ae.getSource().equals(buttonConnectServer)) {
			try {
				EncoderDecoder ed = new VispEncoderDecoder();
				client = new Client(ed);
				client.connect(ip.getText(), Integer.parseInt(port.getText()));
			} catch (ViSeLabException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	@Override
	public double[] compute(ImageProcessing image) {

		DataPackage dn = new DataPackage();
		/*
		 * dn.setSequence(1); dn.setType("DESIRED"); dn.setSequence(3);
		 * dn.addValue(123456.123456); dn.addValue(100);
		 * dn.addValue(222222.124124234321412341234234);
		 */
		// client.encodeAndSendData(dn);

		Vector2[] desiredPoints = image.getTargetCoordinateList();
		dn.setType("DESIRED");
		dn.setSequence(12);
		for (int i = 0; i < 4; i++) {
			dn.addValue(desiredPoints[i].x);
			dn.addValue(desiredPoints[i].y);
			dn.addValue(50);
		}
		client.encodeAndSendData(dn);

		dn.clean();
		dn.setType("CURRENT");
		dn.setSequence(12);
		Vector2[] currentPoints = image.getImageCoordinateList();
		for (int i = 0; i < 4; i++) {
			dn.addValue(currentPoints[i].x);
			dn.addValue(currentPoints[i].y);
			dn.addValue(100);
		}
		client.encodeAndSendData(dn);

		dn.clean();
		dn.setType("END");
		client.encodeAndSendData(dn);

		List<DataPackage> dataList = null;
		try {
			dataList = client.receiveAndDecodeData();
		} catch (ViSeLabException e) {
			e.printStackTrace();
		}
		double[] vel = null;
		if (dataList != null) {
			DataPackage data = dataList.get(0);
			vel = new double[3];
			vel[0] = data.getValue(2);
			vel[1] = data.getValue(3);
			vel[2] = data.getValue(4);
		}
		return vel;
	}

	@Override
	public void cameraFeaturePointsChanged(ImageProcessing i) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void cameraFeaturePointsChanged(ImageSource i) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public double[] compute(ImageSource image) {
		// TODO Auto-generated method stub
		return null;
	}

}